angular.module("surpermarket",[])
.config(["$stateProvider",function($stateProvider){
	$stateProvider.state("/surpermarket",{
		url:"/surpermarket",
		templateUrl:"html/surpermarket/surpermarket.html",
		controller:"surpermarketController"
	})
}])
.controller("surpermarketController",function($scope,$http,$rootScope){
	//控制显示隐藏
	$scope.visible = {
		show:false,
		show1:false
	}
	$scope.toggle = function(num,$event){
		if(num == 1){
			$scope.visible.show1 = false;
			$scope.visible.show = !$scope.visible.show;
		}else if(num == 2){
			$scope.visible.show = false;
			$scope.visible.show1 = !$scope.visible.show1;
		}
		console.log(1);
		if(event.stopPropagation){
			event.stopPropagation();
		}
	}
	$scope.hideNav = function(){
		$scope.visible.show = false;
		$scope.visible.show1 = false;
	}
	$scope.names = ["热销榜","新品尝鲜","优选水果","卤味熟食","牛奶面包","饮料酒水","休闲零食","方便速食","粮油调味","生活用品","冰激凌"]
	// $scope.sliderShow = {show:false};
	var num = 0;
	$scope.slider = function(index){
		$scope.num = index;
		// $scope.sliderShow.show = !$scope.sliderShow.show;
		if(index == 0){
			//获取商品信息
			$http.get("html/surpermarket/node/sale.json").then(function(response){
				$scope.data = response.data.data;
			})
		}else if(index == 1){
			$http.get("html/surpermarket/node/bread.json").then(function(response){
				$scope.data = response.data.data;
			})
		}else if(index == 2){
			$http.get("html/surpermarket/node/fruit.json").then(function(response){
				$scope.data = response.data.data;
			})
		}else if(index == 3){
			$http.get("html/surpermarket/node/hot.json").then(function(response){
				$scope.data = response.data.data;
			})
		}else if(index == 4){
			$http.get("html/surpermarket/node/reserve.json").then(function(response){

				$scope.data = response.data.product;
			})
		}else if(index == 9){
			$http.get("html/surpermarket/node/seckill.json").then(function(response){
				$scope.data = response.data.product;
			})
		}
	}
	$scope.changeClass = function(index){
		return index == $scope.num?"slider":"";
	}
	//获取商品信息
	$http.get("html/surpermarket/node/sale.json").then(function(response){
		$scope.data = response.data.data;
	})

	//筛选信息
	$scope.sort = function(type){
		$scope.data.sort(function(first,second){
			return first[type]*1>second[type]*1?1:-1;
		})
	}
	$scope.sort2 = function(type){
		$scope.data.sort(function(first,second){
			return first[type]*1>second[type]*1?-1:1;
		})
	}
	$scope.sort3 = function(type){
		$http.get("html/surpermarket/node/sale.json").then(function(response){
			$scope.data = response.data.data;
		})
	}

//	//购物车
//	//点击加号
//	$scope.nn = 0;
//	$scope.add = function(index){
//		$scope.nn = index;
//		for(var i=0;i<$rootScope.cartData.length;i++){
//			if($rootScope.cartData[i].name==this.item.name){
//				$rootScope.cartData[i].num++;
//				return;
//			}
//		}
//		this.item.num=1;
//		$rootScope.cartData.push(this.item);
//		
//	}
//	//点击减号
//	$scope.remove = function(){
//		for(var i=0;i<$rootScope.cartData.length;i++){
//			if($rootScope.cartData[i].name==this.item.name){
//				$rootScope.cartData[i].num--;
//				if($rootScope.cartData[i].num==0){
//					$rootScope.cartData.splice(i,1);
//				}
//			}
//		}
//	}
//	$scope.jj = function(index){
//		return index==$scope.nn?true:false;
//	}
	//点击加号
	$scope.addcart = function(shop){
		var boll = false;
		for(var i = 0;i < $rootScope.cartData.length;i++){
			if($rootScope.cartData[i]["id"] == shop['id']){
				$rootScope.cartData[i].count++;
				boll = true;
				break;
			}
		}
		if(!boll){
			shop.count=1;
			shop.shopBuy = false;
			$rootScope.cartData.push(shop)
		}
	}
	//点击减号
	$scope.remove = function(shop){
		for (var i = 0; i < $rootScope.cartData.length; i++) {
			if($rootScope.cartData[i]["id"] == shop["id"]){
				$rootScope.cartData[i]["count"]--
				if($rootScope.cartData[i]["count"]==0){
					$rootScope.cartData.splice(i,1);
				}
				break;
			}
		}
	}
	//单个商品数量
	$scope.ss = function(shop){
		for (var i = 0; i < $rootScope.cartData.length; i++) {
			if($rootScope.cartData[i]["id"] == shop["id"]){
				return $rootScope.cartData[i]["count"]
				break;
			}
		}
	}
	
	$scope.cartData=function(){
		return $rootScope.cartData;
	}
	//总计	
	$scope.total=function(){
		var sum=0;
		for(var i=0;i<$rootScope.cartData.length;i++){
			sum+=($rootScope.cartData[i]['price']*$rootScope.cartData[i]['count']);					
		}
		return sum;	
	}
	
})
